﻿using Hetang.Areas.User.Models.Classes;
using Hetang.Areas.User.Models.Helper;
using Hetang.Models.Authorize;
using Hetang.Models.DataContext;
using Hetang.Models.Helper;
using Hetang.Models.Interface;
using Hetang.Models.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace Hetang.Areas.User.Controllers
{
    [Authorize(Users = "company")]
    [CustomAuthorizeAttribute]
    public class CompanyController : Controller
    {
        IDatabaseRepository _dr;
        ISessionRepository _sr;

        public CompanyController() : this(new DatabaseRepository(), new SessionRepository()) { }

        public CompanyController(IDatabaseRepository dr ,ISessionRepository sr)
        {
            _dr = dr;
            _sr = sr;
        }

        public ActionResult Index()
        {
            tbl_Company company = _dr.GetCompanyByAccountId();
            if (company != null)
            {
                company.Introduce = company.Introduce.Replace("<br />", "\r\n");
                return View(company);
            }
            else
            {
                return RedirectToAction("index", "login", new { area = "" });
            }
        }

        [HttpPost]
        public ActionResult Index(tbl_Company company)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (_sr.GetCompanyId() != 0)
                    {
                        _dr.UpdateCompany(company);
                    }
                    else
                    {
                        _dr.AddCompany(company);                        
                    }                    

                    ViewBag.Info = "操作成功！";
                }
                catch
                {
                    ViewBag.Error = "系统似乎有点忙，请稍后再尝试操作！";                    
                }
            }
            return View(company);
        }

        public ActionResult Create()
        {
            return View();
        }

        public ActionResult Manage()
        {
            return View(_dr.GetJobList());
        }

        public ActionResult Publish()
        {
            var company = _dr.GetCompanyById();
            if (company != null)
            {
                tbl_Job job = new tbl_Job()
                {
                    Address = company.Address,
                    Contacts = company.Contacts,
                    Department = company.Department,
                    Fax = company.Fax,
                    Phone = company.Phone,
                    QQ = company.QQ
                };

                return View(job);
            }
            else
            {
                return RedirectToAction("index");
            }            
        }

        [HttpPost]
        public ActionResult Publish(tbl_Job job)
        {
            TempData.Clear();

            if (ModelState.IsValid)
            {
                try
                {                    
                    if (!_dr.CheckRepeatOfJob(job.JobName.Trim()))
                    {
                        if (_dr.GetNumsOfJob() < 10)
                        {
                            _dr.InsertJob(job);
                            _dr.Save();

                            TempData.Add("Info", "发布成功！");
                        }
                        else
                        {
                            TempData.Add("Error", "系统暂时限制每个账号只能发布10份工作，当前添加操作无效！如有任何疑问，请与管理员联系，谢谢！");                            
                        }
                    }
                    else
                    {
                        TempData.Add("Error", "你已经发布过这个工作的招聘信息了！请勿重复发布！");                        
                    }
                }
                catch
                {
                    TempData.Add("Error", "系统有点繁忙，请稍后再尝试发布！");                    
                }                
            }
            return RedirectToAction("publish");
        }                

        public ActionResult AddDepartment(string d)
        {
            if (string.IsNullOrEmpty(d))
            {
                return Content("你还没有输入部门名称！");
            }
            else
            {
                try
                {
                    tbl_Company company = _dr.GetCompanyByAccountId();
                    if (company != null)
                    {
                        if (!string.IsNullOrEmpty(company.Department))
                        {
                            List<dt_Department> dpm = JsonHelper.parse<List<dt_Department>>(company.Department);

                            //判断是否有重复数据
                            bool repeat = false;
                            foreach (var temp in dpm)
                            {
                                if (temp.Name.Equals(d.Trim()))
                                {
                                    repeat = true;
                                    break;
                                }
                            }
                            if (!repeat)
                            {
                                dpm.Add(new dt_Department() { Name = d.Trim() });
                                company.Department = JsonHelper.stringify(dpm);
                                _dr.Save();
                            }
                            else
                            {
                                return Content("不能添加重复数据！");
                            }
                        }
                        else
                        {
                            List<dt_Department> dpm = new List<dt_Department>();
                            dpm.Add(new dt_Department() { Name = d.Trim() });
                            company.Department = JsonHelper.stringify(dpm);
                            _dr.Save();
                        }
                    }
                    else
                    {
                        return Content("系统繁忙，稍后再添加吧！");
                    }
                }
                catch
                {
                    return Content("系统繁忙，稍后再添加吧！");
                }
                return Content("ok");
            }
        }

        public ContentResult DeleteJob(int jid)
        {
            try
            {
                if (_dr.DeleteJobByJobId(jid))
                {
                    _dr.Save();

                    return Content("ok");
                }
                else
                {
                    return Content("系统繁忙！");
                }                
            }
            catch
            {
                return Content("系统繁忙！");
            }
        }

        public ActionResult EditJob(int? id)
        {
            if (id.HasValue)
            {
                if (id.Value.ToString().Length >= 7)
                {
                    tbl_Job job = _dr.GetJobByJobId(id);                    

                    if (job != null)
                    {
                        return View(job);
                    }
                    else
                    {
                        return RedirectToAction("manage");
                    }
                }
                else
                {
                    return RedirectToAction("manage");
                }                
            }
            else
            {
                return RedirectToAction("manage");
            }            
        }

        [HttpPost]
        public ActionResult EditJob(tbl_Job job)
        {
            TempData.Clear();

            if (ModelState.IsValid)
            {
                try
                {
                    if (!_dr.CheckRepeatOfJob(job.JobName.Trim()))
                    {
                        if (_dr.GetNumsOfJob() < 10)
                        {
                            _dr.InsertJob(job);
                            _dr.Save();

                            TempData.Add("Info", "发布成功！");
                        }
                        else
                        {
                            TempData.Add("Error", "系统暂时限制每个账号只能发布10份工作，当前添加操作无效！如有任何疑问，请与管理员联系，谢谢！");
                        }
                    }
                    else
                    {
                        TempData.Add("Error", "你已经发布过这个工作的招聘信息了！请勿重复发布！");
                    }
                }
                catch
                {
                    TempData.Add("Error", "系统有点繁忙，请稍后再尝试发布！");
                }
            }
            return View();
        }

        public ActionResult Photo()
        {
            return View();
        }
    }
}
